#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

import os
import datetime
FTPFileName='/opt/coopgis/fromgps/anonymousftproot_OtherReadOnly/incomingftpallpermissions/Locations.csv'
import shutil

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime
#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopgis/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
import djangoproject
from djangoproject import settings
from djangoproject.models import *
#********************Setup psycopg2 and django section********************

from django.template import Context, loader
from django.http import HttpResponse
from django import forms
import os
import datetime
import djangoproject.settings

def ReadFileToString(FileName):
    File=open(FileName)
    FileResultsList=[]
    for char in File.read():
        FileResultsList.append(char)
    File.close()
    Result=''.join(FileResultsList)
    return Result

def LeadingZero(String, FinalLength=2):
    Multiplier=FinalLength - len(String)
    if(Multiplier < 0):
        Multiplier = 0
    ZeroList = '0' * Multiplier  
    return ZeroList + String

def StringToDate(DateString):
    """Attempts To Convert:
    'YYYY-MM-DD' to a Date 
    'MM/DD/YYYY', 'MM/YYYY', or 'YYYY' to a Date"""
    if(not DateString):
        return None
    if(DateString.find('-') <> -1):
        Split=DateString.split('-')
        Year=int(Split[0])
        Month=int(Split[1])
        Day=int(Split[2][:2])
    elif(DateString.find('/') <> -1):        
        Split=DateString.split('/')
        if(len(Split) == 3):
            Year=int(Split[2])
            Month=int(Split[0])
            Day=int(Split[1])
        elif(len(Split) == 2):
            Year=int(Split[1])
            Month=int(Split[0])
            Day=1
        else:
            Year=int(Split[0])
            Month=1
            Day=1
    else:
        Year=int(DateString)
        Month=1
        Day=1
    return datetime.date(Year, Month, Day)

def StringDateTimeToDateTime(DateTimeString):
    """Attempts To Convert:
    'YYYY-MM-DD HH-MM-SS.SSS...' to a DateTime,
    returns None if it fails,
    for example: 
               '2008-09-26 15:05:43.222615'
    to:  datetime.datetime(2008, 9, 26, 15, 5, 43, 222615)"""
    if(not DateTimeString):
        return None
    try:
        Year=int(DateTimeString[:4])
        Month=int(DateTimeString[5:7])
        Day=int(DateTimeString[8:10])
        Hour=int(DateTimeString[11:13])
        Minute=int(DateTimeString[14:16])
        Second=int(DateTimeString[17:19])  
        PartialSeconds=int(DateTimeString[20:])
        return datetime.datetime(Year, Month, Day, Hour, Minute, Second, PartialSeconds)
    except:
        return StringToDate(DateTimeString)

